/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dopdf.document.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * The class that represents a collection of any type.
 * @param <T> the generic type. Could be any class such as AgreementItem or Reservation
 * @author deep
 */
public class Collection<T> implements Serializable {

	/** The entities. */
	private final java.util.Collection<T> entities;

	/**
	 * Instantiates a new collection.
	 *
	 * @param entities the entities
	 */
	public Collection(final java.util.Collection<T> entities) {
		this.entities = entities;
	}

	/**
	 * Adds the entity to the list of entities held by this collection.
	 *
	 * @param entity to add.
	 */
	public void add(final T entity) {
		entities.add(entity);
	}

	/**
	 * Returns true if the entity is contained in the collection.
	 *
	 * @param entity to find in the collection.
	 * @return true, if entity was found in the collection.
	 */
	public boolean contains(final T entity) {
		return entities.contains(entity);
	}

	/**
	 * Removes the entity from the collection.
	 *
	 * @param entity to remove.
	 */
	public void remove(final T entity) {
		entities.remove(entity);
	}

	/**
	 * Return entities if condition is true.
	 *
	 * @param evaluator the evaluator
	 * @return the list
	 */
	public List<T> returnEntitiesIfConditionIsTrue(final ConditionEvaluator<T> evaluator) {
		final List<T> matchingEntities = new ArrayList<T>();
		for (final T entity : entities) {
			if (evaluator.evaluate(entity)) {
				matchingEntities.add(entity);
			}
		}
		return matchingEntities;
	}

	/**
	 * Returns the Size of the collection.
	 *
	 * @return the int value representing the size of the collection.
	 */
	public int size() {
		return entities.size();
	}

	/**
	 * Do in loop.
	 *
	 * @param evaluator the evaluator
	 */
	public void doInLoop(final ExpressionEvaluator<T> evaluator) {
		for (final T entity : entities) {
			evaluator.evaluate(entity);
		}
	}

    /**
     * Return entity if condition is true.
     *
     * @param evaluator the evaluator
     * @return the t
     */
    public T returnEntityIfConditionIsTrue(final ConditionEvaluator<T> evaluator) {
        for (final T entity : entities) {
            if (evaluator.evaluate(entity)) {
                return entity;
            }
        }
        return null;
    }

    /**
     * Returns the element at the given index.  This method will rely on the source collection for getting the element
     * at the given index.  If the source collection is of type set then we cannot gaurentee that we returns the same
     * element if called multiple times.
     * @param index the element index.
     * @return The element type at the given index.
     */
    public synchronized T get(final int index) {
        int i=0;
        for(final Iterator<T> iterator = entities.iterator(); iterator.hasNext(); ) {
            final T entity = iterator.next();
            if(i >= index) return entity;
            i++;
        }
        return null;
    }

	/**
	 * Gets the entities.
	 *
	 * @return the entities
	 */
	protected java.util.Collection<T> getEntities() {
		return entities;
	}

	/**
	 * Return false if any condition is false.
	 *
	 * @param evaluator the evaluator
	 * @return true, if successful
	 */
	protected boolean returnFalseIfAnyConditionIsFalse(final ConditionEvaluator<T> evaluator) {
		for (final T entity : entities) {
			if (!evaluator.evaluate(entity)) {
				return false;
			}
		}
		return true;
	}

	/**
	 * Return true if any condition is true.
	 *
	 * @param evaluator the evaluator
	 * @return true, if successful
	 */
	protected boolean returnTrueIfAnyConditionIsTrue(final ConditionEvaluator<T> evaluator) {
		for (final T entity : entities) {
			if (evaluator.evaluate(entity)) {
				return true;
			}
		}
		return false;
	}
}
